﻿using System.Collections;
using System.Collections.Generic;

using strange.extensions.mediation.impl;
using strange.extensions.dispatcher.eventdispatcher.api;
using System;

public class IntergrationMediator : EventMediator
{
    [Inject]
    public IntergrationView IntergrationView { get; set; }

    [Inject]
    public CardModel cardModel { get; set; }
    [Inject]
    public RoundModel RoundModel { get; set; }
    #region 监听方法
    public override void OnRegister()
    {
        RoundModel.PlayerHandler += ActiveBUtton;
        IntergrationView.But_Deal.onClick.AddListener(OnDealClick);
        IntergrationView.But_Grab.onClick.AddListener(OnGrabClick);
        IntergrationView.But_DisGrab.onClick.AddListener(OnDisGrabClick);
        IntergrationView.But_Pass.onClick.AddListener(OnPassClick);
        IntergrationView.But_Play.onClick.AddListener(OnPlayClick);
        dispatcher.AddListener(ViewEvent.CompeletDeal, OnComplete);
        dispatcher.AddListener(ViewEvent.CompletePlayCard, OnCompletePlayCard);
        dispatcher.AddListener(ViewEvent.ReStartGame, OnReStartGame);
    }


    public override void OnRemove()
    {
        RoundModel.PlayerHandler -= ActiveBUtton;
        IntergrationView.But_Deal.onClick.RemoveListener(OnDealClick);
        IntergrationView.But_Grab.onClick.RemoveListener(OnGrabClick);
        IntergrationView.But_DisGrab.onClick.RemoveListener(OnDisGrabClick);
        IntergrationView.But_Pass.onClick.RemoveListener(OnPassClick);
        IntergrationView.But_Play.onClick.RemoveListener(OnPlayClick);
        dispatcher.RemoveListener(ViewEvent.CompeletDeal, OnComplete);
        dispatcher.RemoveListener(ViewEvent.CompletePlayCard, OnCompletePlayCard);
        dispatcher.RemoveListener(ViewEvent.ReStartGame, OnReStartGame);
    }




    #endregion
    #region 回到函数

    /// <summary>
    /// 不抢地主的回调
    /// </summary>
    private void OnDisGrabClick()
    {
        IntergrationView.DeActiveAll();
        GrabAgrs e = new GrabAgrs();
        if (UnityEngine.Random.Range(2, 4) == 2)
        {
            e.cType = CharacterType.ComputerLeft;
        }
        else
        {
            e.cType = CharacterType.ComputerRight;
        }
        dispatcher.Dispatch(CommandEvent.Command_Grab, e);
        
    }

    /// <summary>
    /// 抢地主的回调
    /// </summary>
    private void OnGrabClick()
    {
        IntergrationView.DeActiveAll();
        GrabAgrs e = new GrabAgrs() { cType = CharacterType.Player };
        dispatcher.Dispatch(CommandEvent.Command_Grab,e);
        
    }

    /// <summary>
    /// 发牌的回调
    /// </summary>
    private void OnDealClick()
    {
        dispatcher.Dispatch(CommandEvent.Command_RegisterDeal);
        IntergrationView.DeActiveAll();
    }
    /// <summary>
    /// 发牌结束的回调
    /// </summary>
    /// <param name="payload"></param>
    private void OnComplete(IEvent payload)
    {
        IntergrationView.ActiveGrab();
    }
    /// <summary>
    /// 激活按钮
    /// </summary>
    /// <param name="CanPass"></param>
    public void ActiveBUtton(bool CanPass)
    {
        IntergrationView.ActivePlayAndPass(CanPass);
    }
    /// <summary>
    /// 不出回调
    /// </summary>
    private void OnPassClick()
    {
        dispatcher.Dispatch(CommandEvent.Command_Pass);
        IntergrationView.DeActiveAll();
    }

    /// <summary>
    /// 出牌回调
    /// </summary>
    private void OnPlayClick()
    {
        dispatcher.Dispatch(ViewEvent.RegisterPlay);
    }
    /// <summary>
    /// 出牌完成的回调
    /// </summary>
    /// <param name="payload"></param>
    private void OnCompletePlayCard(IEvent evt)
    {
        
        PlayCardArgs e = evt.data as PlayCardArgs;
        if (RoundModel.CurrentCharacter == CharacterType.Player)
        {
            IntergrationView.ActivePlayAndPass(true);
        }
        else
        {
            IntergrationView.DeActiveAll();
        }
    }
    /// <summary>
    /// 重新开始游戏
    /// </summary>
    private void OnReStartGame()
    {
        IntergrationView.DeActiveAll();
        IntergrationView.ActiveDeal();
    }
    #endregion
}
